<template>
	<uni-loading v-if="loading"></uni-loading>
	<view class="body" v-else>
		<!--  顶部  -->
		<top-tab :stockNameProp="stockName"  :options="stockList" :locationName="locationName" @stockChange="stockChange"
			@handleBarnChoose="handleBarnChoose"></top-tab>
		<!--  列表  -->
		<template v-if="purchaseList && purchaseList.length > 0">
			<uni-swipe-action>
				<uni-swipe-action-item   v-for="item in purchaseList">
					<view class="panel" >
						<view class="panel_title">
							<view class="panel_title_left">{{ item.materialName }}</view>
						</view>
						<view class="panel_content">
							<view class="panel_content">
								<view class="panel_content_row">
									<view class="panel_content_row_item">
										<view class="panel_content_row_item_title">件号：</view>
										<view class="panel_content_row_item_value">{{ item.fspecification }}</view>
									</view>
								</view>
							</view>
							<view class="panel_content_row">
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">名称：</view>
									<view class="panel_content_row_item_value">{{ item.fmaterialname }}</view>
								</view>
								<view class="panel_content_row_item">
									<view class="panel_content_row_item_title">仓位：</view>
									<view class="panel_content_row_item_value">{{ item.fsrcStockLocId }}</view>
								</view>
							</view>
						</view>
						<view class="panel_dashboard">
							<view class="panel_dashboard_item">
								<view class="panel_dashboard_item_number success">{{ item.flot }}</view>
								<view class="panel_dashboard_item_value">批号</view>
							</view>
							<view class="panel_dashboard_item">
								<view class="panel_dashboard_item_number error">{{ item.fqty }}{{ item.funitname }}
								</view>
								<view class="panel_dashboard_item_value">重量</view>
							</view>
						</view>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</template>
		<uni-empty-info v-else label="暂无物料信息"></uni-empty-info>

		<!--  底部按钮  -->
		<view class="footerPanel">
			<view class="footerPanel_body">
				<button @click="handleSuccess" class="footerBtn scan" type="primary"><i
						class="iconfont icon-duihao btn_icon"></i>完成
				</button>
			</view>
		</view>
<!-- 全局遮罩：在 butClick 为 true 时显示，阻止所有交互，显示转圈提示 -->
		<view v-if="butClick" class="global-mask">
		  <view class="loader-box">
		    <view class="loader"></view>
		    <view class="loader-text">审核中，请稍候...</view>
		  </view>
		</view>
		<BarnPopup ref="barnPopup" @success="locationScanningSuccess"></BarnPopup>
	</view>
</template>
<script>
	import topTab from "@/components/top-tab/top-tab"
	import uniLoading from "../../vBase/components/uni-loading/uni-loading"
	import uniEmptyInfo from "../../vBase/components/uni-empty-info/uni-empty-info"
	import BarnPopup from "@/components/popup/barnPopup"
	import{getStockListByUser,
	getTransferTemp,
	createTransfer
		}  from  '@/utils/api.js'
	export default {
		components: {
			topTab,
			uniLoading,
			uniEmptyInfo,
			BarnPopup
		},
		data() {
			return {
				loading: false,
				purchaseList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#FF4E74'
					}
				}],
				scaningShow: false,
				scanNo: null,
				butClick : false,
				isFouce: false,
				stockList: [],
				stockNo: 'CK004',
				stockName:'机加成品库',
				locationNo: '',
				locationName: '',
				sourceStockNo: null,
				sourceLocationNo: null
			}
		},
		/**
		 * 返回指定页面(onBackPress函数放置)
		 *
		 * @param e
		 */
		onLoad(e) {
			// this.getStockList();
		
		},
		onUnload() {
			uni.$off('deleteItem');
		},
		mounted() {
			this.getStockList()
			this.initData()
		},
		methods: {
			//仓位扫码成功后返回
			locationScanningSuccess(e){
				this.locationNo =e
				this.locationName =e
				
			},
			/** 查询仓库*/
			getStockList() {
				getStockListByUser({}).then(res =>{
					this.stockList = res.data.data.dataList
				})
			},
			/** 初始化数据 */
			initData() {
				
				this.getDataList()
			},

			/** 获取数据 */
			getDataList() {
				getTransferTemp({}).then(res =>{
					this.purchaseList = res.data.data.dataList;
				})
			},

			/** 完成新增 */
			async handleSuccess() {
				try {
					let trayNumber = uni.getStorageSync("transfer") ?? ""
					if (!this.stockNo) {
						uni.showToast({
							title: '请选择仓库',
							icon: 'none'
						});
						return;
					}
					if (!this.locationNo) {
						uni.showToast({
							title: '请选择仓位',
							icon: 'none'
						});
						return;
					}
					if (!this.purchaseList || !this.purchaseList.length) {
						uni.showToast({
							title: '请添加物料信息',
							icon: 'none'
						});
						return;
					}
					let _this = this;
					uni.showModal({
						title:'提示',
						content:'确认审核',
						success(res) {
							
							if (res.confirm){
								_this.butClick = true; // 禁止其他操作
								uni.showLoading({
									title: "审核中...",
									mask: true
								});
								let param = {
									stockNo : _this.stockNo,
									locationNo : _this.locationNo
								}
								
								createTransfer(param).then(res =>{
									if (res.data.status == 200){
										uni.hideLoading();
										_this.butClick = false;
										uni.showToast({
											title: "调拨完成",
											icon: "none"
										})
										setTimeout(() => {
											uni.navigateBack({
												delta: 2
											})
										}, 2000)
									}
								}).catch(err => {
									// 接口异常恢复按钮和 loading
									uni.hideLoading();
									_this.butClick = false;
									// uni.showModal({
									// 	title:'提示',
									// 	content:'接口异常,请联系谭工'
									// })
		                 
		
		                    
		                });
							}
						}
					})
					
					
				} finally {}

			},

	

			/**
			 * 选择仓库
			 * @param e
			 */
			stockChange(e) {
				this.stockNo = this.stockList[e].stockNo
				// this.initData()
			},

			/**
			 * 选择仓位
			 * @param e
			 */
			stockLocation(e) {
				this.locationNo = e.locationNo
				this.locationName = e.locationName
			},

			/**
			 * 选择
			 */
			handleBarnChoose() {
				this.$refs.barnPopup.open();
			},


			

		}
	}
</script>
<style lang="scss" scoped>
	/* 全局遮罩 (覆盖整个页面，挡住所有点击) */
	.global-mask {
	  position: fixed;
	  left: 0;
	  top: 0;
	  right: 0;
	  bottom: 0;
	  z-index: 9999;
	  background: rgba(0,0,0,0.45);
	  display: flex;
	  align-items: center;
	  justify-content: center;
	}
	
	/* loader 容器 */
	.loader-box {
	  display: flex;
	  flex-direction: column;
	  align-items: center;
	  justify-content: center;
	  padding: 20px 30px;
	  background: rgba(255,255,255,0.95);
	  border-radius: 8px;
	}
	
	/* CSS 简易转圈 */
	.loader {
	  width: 44px;
	  height: 44px;
	  border-radius: 50%;
	  border: 4px solid rgba(0,0,0,0.1);
	  border-top-color: #ffffff;
	  border-right-color: #ffffff;
	  animation: spin 1s linear infinite;
	  margin-bottom: 8px;
	}
	
	.loader-text {
	  font-size: 14px;
	  color: #333;
	}
	
	/* 动画 */
	@keyframes spin {
	  from { transform: rotate(0deg); }
	  to { transform: rotate(360deg); }
	}
	
	@import "../../static/css/common";
</style>
